package com.tofine.smarthome.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.tofine.smarthome.R;
import com.tofine.smarthome.base.BaseActivity;
import com.tofine.smarthome.model.Device;
import com.tofine.smarthome.util.DeviceUtils;

import butterknife.Bind;

/**
 * @author ddMax
 * @since 2016/5/5 17:37
 * 说明：设备信息Activity
 */
public class DeviceInfoActivity extends BaseActivity {
    public static final String TAG = DeviceInfoActivity.class.getSimpleName();

    public static final int MODE_ADD = 0;
    public static final int MODE_EDIT = 1;

    @Bind(R.id.device_image) ImageView mDeviceImage;
    @Bind(R.id.device_title) TextView mDeviceTitle;
    @Bind(R.id.device_category) TextView mDeviceCategory;
    @Bind(R.id.device_intro) TextView mDeviceIntro;
    @Bind(R.id.device_next) AppCompatButton mBtnNext;

    private Device device;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_info;
    }

    @Override
    protected int getActionBarCustomView() {
        return R.layout.actionbar_info;
    }

    @Override
    protected int getActionBarTitle() {
        return R.string.title_activity_device_info;
    }

    @Override
    protected boolean hasBackButton() {
        return true;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);

    }

    @Override
    public void initView() {
        final Bundle bundle = getIntent().getExtras();
        if (bundle.getInt("mode") == MODE_EDIT) {
            // 显示和编辑设备模式
            device = (Device) bundle.getSerializable("device");
            Log.d(TAG, device.toString());
            // 隐藏下一步Button
            mBtnNext.setVisibility(View.INVISIBLE);
        } else {
            // 扫一扫后的添加模式
            String scanResult = bundle.getString("scanResult");
            device = DeviceUtils.getDeviceFromQRCode(scanResult);
            if (device == null) {
                Toast.makeText(DeviceInfoActivity.this, "无法识别二维码，请重试", Toast.LENGTH_SHORT).show();
                finish();
                return;
            }
            // 设置下一步监听
            mBtnNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DeviceInfoActivity.this, DeviceAddToRoomActivity.class);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, MODE_ADD);
                }
            });
        }
        Picasso.with(this).load(device.image).into(mDeviceImage);
        mDeviceTitle.setText(device.name);
        mDeviceCategory.setText(device.category == null ? "" : device.category.name);
        mDeviceIntro.setText(device.intro);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case MODE_ADD:
                if (resultCode == RESULT_OK) {
                    Bundle bundle = data.getExtras();
                    bundle.putSerializable("new_device", device);
                    Intent intent = new Intent();
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;
            default:
                break;
        }
    }
}
